<?php
/*
	代理商商品
	有个问题。代理商从A处代理一个商品，后来发现从B处代理的价格更低！自然，代理商想要把上家换成B，这时该当如何操作？
	还有个问题，代理商是否支持自定义商品的一些信息呢？
	例如，自定义标题，图片，介绍！
	还有个问题。代理商加价后，高价格带来的责任该由谁承担？也应该由代理商。
	因此，应该在尽量多的地方显示代理商的信息，提示用户，是从代理商那里购买，而非直接从平台。
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$member=$this->model('member')->checkmember('',true);
if($member['dian_mid']){
	exi('您是业务员！无权操作！','error','/yewu/');
}
if($_RQ['op']=='main'){
	$return['list']=array();
	$return['params']=$return['oldparams']=array('pluginname'=>'jy_area_dai');
	$p_group=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isrecommand'=>'1','isshow'=>'1','parentid'=>'0'),'','paixu asc');
	//子分类
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$params['saletype likes']=array('1');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','image','price','total','active_plugin','active_id');
	if($p_group){
		foreach($p_group as &$item){
			$item['c_group']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>'1','isrecommand'=>'1','parentid'=>$item['id']),'','paixu asc');
		}
	}
	$return['p_group']=$p_group;
	$return['params']['pid']=0;
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
	);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}else{
		//有关键词时，分类ID不检索！
		if(($_RQ['pcate'] || $_RQ['pid']) && $_RQ['pid']!=-1){
			$pcate=$_RQ['pcate']?intval($_RQ['pcate']):intval($_RQ['pid']);
			$params['pcate']=$pcate;
			$return['params']['pcate']=$pcate;
		}
		if($_RQ['ccate'] || $_RQ['cid']){
			$ccate=$_RQ['ccate']?intval($_RQ['ccate']):intval($_RQ['cid']);
			$params['ccate']=$ccate;
			$return['params']['ccate']=$ccate;
		}
	}
	if($params['ccate'] || $params['pcate'] || $_RQ['keyword']){
		$goodsids=pdo_getall('jy_weishop_goods',$params,'id','id');
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
	);
	if($goodsids){
		$goodsids=array_keys($goodsids);
		$params['goods_id']=$goodsids;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize =10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$orderby='id desc';
	$parameter=array(
			'params'=>$params,
			'limit'=>array($pindex,$psize),
			'fields'=>$fields,
			'orderby'=>$orderby,
	);
	$parameter['params']['ignore']=0;
	$list=$this->get_list('goods',$parameter,$total);
	if(!$list){
		exi('none');
	}else{
		foreach($list as &$itemv){
			$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$itemv['goods_id']),array('title','merchid','status','total','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit'));
			$itemv=array_merge($goods,$itemv);
			$itemv['members']=$members;			
			if($this->plugin_exists('jy_merch')){
				$itemv['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$itemv['merchid']),'merchname');
			}
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['goods_id'],),'id');
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['goods_id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['goods_id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			if($_SESSION['is_tomedia']){
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
				$itemv['video']=$itemv['video']?tomedia($itemv['video']):'';
			}
			$itemv['id']=$itemv['goods_id'];
			$itemv['active_plugin']='jy_area_dai';
			$itemv=model('jy_weishop/activity')->get_upgoods($itemv);
		}
	}
	exi($list);
}